window.onload = function() {
    // 判断是否登录
    // var userid = getCookie('userid')
    // var username = getCookie('username')
    let userid=localStorage.getItem('userid')
    let username=localStorage.getItem('username')
    if(!userid || !username) {
        popup.msg('请先登录！', () => {
            localStorage.setItem('url', location.href)
            location.href = './login.html'
        })
        return false
    }
    // 就餐方式切换
    var dining = document.querySelector('.dining')
    var mode = null
    dining.onclick = e => {
        var target = e.target
        if(target.tagName === 'SPAN') {
            for(var a = 0; a < dining.children.length; a++) {
                dining.children[a].className = ''
            }
            target.className = 'active'
            mode = target.innerText
        }
    }
    // 获取购物车数据
    let cartlist = document.querySelector('.cartlist')
    let topay = document.querySelector('.topay')
    request({
        url: 'http://localhost:3000/front/api/cart/getall/' + userid,
        headers: {
            authorization: localStorage.getItem('token')
        }
    }).then(carts => {
        // console.log(carts);
        let {error_code, msg} = carts
        if(error_code === 0) {
            topay.setAttribute('data-id', carts.data.map(item => item._id).join(','))
            let menuids = carts.data.map(item => item.menuid).join(',')
            // 获取用户信息请求
            let getuserinfo = request({
                url: 'http://localhost:3000/front/api/users/userinfo/' + userid,
                headers: {
                    authorization: localStorage.getItem('token')
                }
            })
            // 获取购物车中商品请求
            let getcartmenus = request({
                method: 'post',
                url: 'http://localhost:3000/front/api/cart/getmenus',
                data: {
                    menuids
                },
                headers: {
                    authorization: localStorage.getItem('token')
                }
            })
            Promise.all([getuserinfo, getcartmenus]).then(arr => {
                // console.log(arr);
                let [userinfo, cartmenus] = arr
                // 渲染用户信息
                document.querySelector('.username').innerText = userinfo.data.username
                // 渲染购物车商品
                let carthtml = ''
                cartmenus.data.forEach(item => {
                    item.speandkind = carts.data.find(v => v.menuid === item._id).speandkind
                    item.num = carts.data.find(v => v.menuid === item._id).num
                    carthtml += `
                        <div>
                            <img src="${item.imgpath}" alt="">
                            <div class="name">
                                <b>${item.name}</b>
                                <br>
                                <span>${item.speandkind}</span>
                            </div>
                            <div class="number">x${item.num}</div>
                            <div class="price">￥<span>${item.price.toFixed(2)}</span></div>
                        </div>
                    `
                })
                cartlist.innerHTML += carthtml
                // 样式
                var img = document.querySelector('.cartlist>div img')
                // console.log(img.offsetWidth);
                img.style.height = img.offsetWidth + 'px'
                // 计算总价
                let totalprice = 0
                document.querySelectorAll('.price>span').forEach(ele => {
                    totalprice += parseFloat(ele.innerText)
                })
                document.querySelector('.totalprice').innerText = document.querySelector('.totalprice1').innerText = '￥' + totalprice.toFixed(2)
            })
            // 支付
            topay.onclick = function() {
                request({
                    url: 'http://localhost:3000/front/api/orders/create',
                    data: {
                        ids: this.dataset.id,
                        mode,
                        totalprice: document.querySelector('.totalprice').innerText,
                        userid
                    },
                    method: 'post',
                    headers: {
                        authorization: localStorage.getItem('token')
                    }
                }).then(res => {
                    let {error_code, msg} = res
                    if(error_code === 0) {
                        location.href = './order.html'
                    }
                })
            }
        }
    })

    
}